<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>用户后台管理</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.4.1/dist/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">

</head>
<body>
    <center>
        <table id="group" class="table table-striped" style="margin-top: 10px">
            <thead>
            <tr>
                <th>序号</th>
                <th>姓名</th>
                <th>性别</th>
                <th>年龄</th>
                <th>学号</th>
                <th>班级</th>
                <th>操作</th>
            </tr>
            </thead>
        </table>
    </center>
    <button type="button" class="btn btn-primary" onclick="addForm()" id="add">添加用户</button>

    <!--添加时用的表单-->
    <form class="form-horizontal" id="addForm" style="display: none">
        <div class="form-group">
            <label for="userId" class="col-sm-2 control-label">id</label>
            <div class="col-sm-3">
                <input disabled type="text" class="form-control" id="userId" placeholder="此处不可修改">
            </div>
        </div>
        <div class="form-group">
            <label for="name" class="col-sm-2 control-label">姓名</label>
            <div class="col-sm-3">
                <input type="text" class="form-control" id="name" placeholder="请输入姓名">
            </div>
        </div>
        <div class="form-group">
            <label for="age" class="col-sm-2 control-label">年龄</label>
            <div class="col-sm-3">
                <input type="text" class="form-control" id="age" placeholder="请输入年龄">
            </div>
        </div>
        <div class="form-group">
            <label for="studyCode" class="col-sm-2 control-label">学号</label>
            <div class="col-sm-3">
                <input type="text" class="form-control" id="studyCode" placeholder="请输入学号">
            </div>
        </div>
        <div class="form-group">
            <label for="studyClass" class="col-sm-2 control-label">班级</label>
            <div class="col-sm-3">
                <input type="text" class="form-control" id="studyClass" placeholder="请输入班级名称">
            </div>
        </div>

        <label class="radio-inline" style="margin-left: 20px ">
            <input type="radio" name="sex" id="sex1" value="男"> 男
        </label>
        <label class="radio-inline" style="margin-left: 20px ">
            <input type="radio" name="sex" id="sex2" value="女"> 女
        </label>

        <div class="form-group">
            <div class="col-sm-offset-2 col-sm-10">
                <button type="button" class="btn btn-primary" onclick="add()">保存</button>
            </div>
        </div>
    </form>
<script src="https://cdn.jsdelivr.net/npm/jquery@3.4.1/dist/jquery.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.4.1/dist/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
<script src="js/jquery.min.js"></script>
<script type="text/javascript">

    $(function () {
        $.ajax({
            type: "GET",
            url: "http://localhost:8080/findAll",
            success: function(msg){
                console.log(msg)
                var groupTable = $("#group");
                for (var i=1;i<=msg.length;i++){
                    groupTable.append('<tr> ' +
                        '<td>' + msg[i-1].id + '</td>' +
                        '<td>' + msg[i-1].name + '</td>' +
                        '<td>' + msg[i-1].sex + '</td>' +
                        '<td>' + msg[i-1].age + '</td>' +
                        '<td>' + msg[i-1].studyCode + '</td>' +
                        '<td>' + msg[i-1].studyClass + '</td>' +
                        '<td>' + '<button type="button" class="btn btn-warning btn-sm" onclick="change('+msg[i-1].id+')">修改</button>&nbsp;&nbsp;&nbsp;&nbsp;<button type="button" class="btn btn-danger btn-sm" onclick="del('+msg[i-1].studyCode+')">删除</button>' + '</td>' +
                        '</tr>')
                }
            }
        });
    });
    var del = function (studyCode) {
        $.ajax({
            type: "GET",
            url: "http://localhost:8080/delete",
            data: "studyCode="+studyCode,
            success: function(msg){
                alert(msg.msg);
                window.location.reload();
            }
        });
    };
    
    var addForm = function () {
        //调出添加表单时把数据清空
        $("#userId").val("");
        $("#name").val("");
        $("#age").val("");
        $("#studyCode").val("");
        $("#studyClass").val("");
        $("[name='sex']:checked").val("");
        $("#addForm").toggle();
    };
    
    var add = function () {
        var id = $("#userId").val();
        var name = $("#name").val();
        var age = $("#age").val();
        var studyCode = $("#studyCode").val();
        var studyClass = $("#studyClass").val();
        var sex = $("input[name='sex']:checked").val();

        $.ajax({
            type: "POST",
            url: "http://localhost:8080/update",
            data: {
                id: id,
                name: name,
                age: age,
                sex: sex,
                studyCode: studyCode,
                studyClass: studyClass
            },
            success: function(res){
                alert(res.msg);
                window.location.reload();
            }
        });
    }

    var change = function (id) {
        $("#addForm").show();
        $.ajax({
            type: "GET",
            url: "http://localhost:8080/findOne",
            data: "id="+id,
            success: function(msg){
                $("#userId").val(msg.id);
                $("#name").val(msg.name);
                $("#age").val(msg.age);
                $("#studyCode").val(msg.studyCode);
                $("#studyClass").val(msg.studyClass);
                $("input:radio[name='sex']").val([msg.sex]);
            }
        });
    }
</script>
</body>
</html>